package controller

import (
	"encoding/json"
	"gitee.com/saxon134/sysmain/api"
	"gitee.com/saxon134/sysmain/conf"
	"gitee.com/saxon134/sysmain/db"
	"gitee.com/saxon134/sysmain/db/models"
	"gitee.com/saxon134/sysmain/errs"
	"github.com/saxon134/go-utils/saData"
	"github.com/saxon134/go-utils/saHttp"
	"net/http"
)

func AccountMenus(w http.ResponseWriter, r *http.Request) {
	api.ResSuccess(w, []map[string]interface{}{
		{
			"id": 1, "pid": 0, "seq": 0, "hidden": true,
			"title":     "修改资料",
			"icon":      "",
			"path":      "/account/modify",
			"component": "views/account/modify.vue",
		},
		{
			"id": 2, "pid": 0, "seq": 2, "hidden": false,
			"title":     "应用状态",
			"icon":      "app",
			"path":      "/app",
			"component": "views/app/app.vue",
		},
		{
			"id": 5, "pid": 0, "seq": 3, "hidden": false,
			"title":     "接口概览",
			"icon":      "api",
			"path":      "/api",
			"component": "views/api/api.vue",
		},
		{
			"id": 4, "pid": 0, "seq": 4, "hidden": false,
			"title":     "调度任务",
			"icon":      "task",
			"path":      "/task",
			"component": "views/task/task.vue",
		},
		{
			"id": 3, "pid": 0, "seq": 3, "hidden": false,
			"title":     "服务资源",
			"icon":      "resource",
			"path":      "/resource",
			"component": "components/common/router-view.vue",
			"children": []map[string]interface{}{
				{
					"id": 31, "pid": 3, "seq": 1, "hidden": false,
					"title":     "ECS",
					"icon":      "",
					"path":      "/resource/ecs",
					"component": "views/resource/ecs.vue",
				},
				{
					"id": 32, "pid": 3, "seq": 2, "hidden": false,
					"title":     "RDS",
					"icon":      "",
					"path":      "/resource/rds",
					"component": "views/resource/rds.vue",
				},
			},
		},
	})
}

func AccountLogin(w http.ResponseWriter, r *http.Request) {
	decoder := json.NewDecoder(r.Body)
	var params map[string]string
	_ = decoder.Decode(&params)
	if params["account"] == "" || params["password"] == "" {
		api.ResError(w, errs.ErrorParams.Error())
		return
	}

	obj := new(models.TblAccount)
	err := db.MySql.Table(models.TBNAccount).Where("account = ?", params["account"]).First(obj).Error
	if db.MySql.IsError(err) {
		api.ResError(w, err.Error())
		return
	}

	if obj.Id <= 0 {
		api.ResError(w, "账户不存在")
		return
	}

	var password = saData.Md5(params["password"]+"sysmain", true)
	if obj.Password != password {
		api.ResError(w, "账户名、密码不匹配")
		return
	}

	if obj.Status != 2 {
		api.ResError(w, "账户已停止访问")
		return
	}

	token, _ := saHttp.JwtGenerate(&api.AccountJwt{Id: obj.Id}, conf.Conf.JwtSecret)
	api.ResSuccess(w, map[string]interface{}{
		"id":       obj.Id,
		"createAt": obj.CreateAt,
		"name":     obj.Name,
		"account":  obj.Account,
		"status":   obj.Status,
		"token":    token,
	})
}

func AccountSave(w http.ResponseWriter, r *http.Request) {
	var obj = new(models.TblAccount)
	var err error

	decoder := json.NewDecoder(r.Body)
	_ = decoder.Decode(obj)
	if obj.Account == "" || obj.Name == "" {
		api.ResError(w, errs.ErrorParams.Error())
		return
	}

	var existed = new(models.TblAccount)

	//新建
	if obj.Id <= 0 {
		err = db.MySql.Table(models.TBNAccount).Where("account = ?", obj.Account).First(existed).Error
		if db.MySql.IsError(err) {
			api.ResError(w, err.Error())
			return
		}

		if existed.Id > 0 {
			api.ResError(w, "该账户已经存在")
			return
		}

		obj.Status = 2
	} else
	//保存信息
	{
		err = db.MySql.Table(models.TBNAccount).Where("id = ?", obj.Id).First(existed).Error
		if db.MySql.IsError(err) {
			api.ResError(w, err.Error())
			return
		}

		if existed.Account != obj.Account {
			api.ResError(w, "账户名不匹配")
			return
		}
	}

	err = db.MySql.Table(models.TBNAccount).Save(obj).Error
	if err != nil {
		api.ResError(w, err.Error())
		return
	}
	api.ResSuccess(w, nil, 0)
}
